﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;
namespace VCN.admin
{
    public partial class NewsManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                this.LoadData();
                this.ReloadUserData();
                Page.Title = "Sửa tin tức";
            }

        }
        private void LoadData()
        {
            UsersDAO v_UserDAO = new UsersDAO();
            DataTable v_dt = v_UserDAO.selectAll2Dt();
            ddl_AuthorId.DataSource = v_dt;
            ddl_AuthorId.DataTextField = "USERNAME";
            ddl_AuthorId.DataValueField = "ID";
            ddl_AuthorId.DataBind();
        }
        private void ReloadUserData()
        {
            string v_id = Request.QueryString["Id"].ToString();
            if (!String.IsNullOrEmpty(v_id))
            {
                NewsDAO v_NewsDAO = new NewsDAO();
                VCN.App.DataTransferObjects.News v_News = new VCN.App.DataTransferObjects.News();
                v_News = v_NewsDAO.SelectById(int.Parse(v_id));
                txt_Title.Text = v_News.Title;
                txt_ShortContent.Text = v_News.ShortContent;
                txt_Content.Text = v_News.Contents;
                try 
	            {
                    ddl_AuthorId.SelectedValue = v_News.AuthorId.ToString();
	            }
	            catch (Exception)
	            {
		
		            throw;
	            }

                txt_Reference.Text = v_News.Reference;
                imgPreview.AlternateText = v_News.NewsImage;
                imgPreview.ImageUrl = string.Format("../home/Images/News/{0}", v_News.NewsImage);
            }
        }
        protected void add_click(object sender, EventArgs e)
        {
            VCN.App.DataTransferObjects.News v_News = new VCN.App.DataTransferObjects.News();
            v_News.Id = int.Parse(Request.QueryString["Id"].ToString());
            v_News.NewsImage = this.doUploadPImage();
            v_News.Reference = txt_Reference.Text;
            v_News.ShortContent = txt_ShortContent.Text;
            v_News.Contents = txt_Content.Text;
            v_News.Title = txt_Title.Text;
            v_News.AuthorId = int.Parse(ddl_AuthorId.SelectedItem.Value);
            v_News.CreatedDate = DateTime.Now;
            NewsDAO v_NewsDAO = new NewsDAO();
            v_NewsDAO.update(v_News);
            Response.Redirect("NewsList.aspx");
        }

        protected void reset_click(object sender, EventArgs e)
        {
            txt_Title.Text = "";
            txt_ShortContent.Text = "";
            txt_Content.Text = "";
            txt_Reference.Text = "";
            imgPreview.AlternateText = "";
            imgPreview.ImageUrl = "";


        }

        protected void cancel_click(object sender, EventArgs e)
        {
            Response.Redirect("NewsList.aspx");
        }
        private string doUploadPImage()
        {
            if (FileUpload.HasFile)
            {
                var v_str_fstorage = string.Format("../home/Images/News/{0}", FileUpload.FileName);
                this.FileUpload.SaveAs(Server.MapPath(v_str_fstorage));
                return FileUpload.FileName;
            }
            return this.imgPreview.AlternateText;
        }
    }
}
